<template>
	<el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible" :close-on-click-modal="false" draggable>
		<el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
			<el-row :gutter="24">
				<el-col :span="24" class="mb20">
					<el-form-item label="feed类型" prop="feedType">
						<el-select v-model="form.feedType" placeholder="请选择feed类型">
							<el-option label="资讯" value="info"></el-option>
							<el-option label="广告" value="advert"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="广告开始时间" prop="begin_time" v-if="form.feedType == 'advert'">
						<el-date-picker type="datetime" placeholder="请选择创建时间" v-model="form.beginTime" :value-format="dateTimeStr"></el-date-picker>
					</el-form-item>
					<el-form-item label="广告结束时间" prop="end_time" v-if="form.feedType == 'advert'">
						<el-date-picker type="datetime" placeholder="请选择创建时间" v-model="form.endTime" :value-format="dateTimeStr"></el-date-picker>
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item label="标题" prop="title">
						<el-input v-model="form.title" placeholder="请输入标题" />
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item label="图片地址" prop="picUrl">
						<span v-for="index in 18" :key="index">
							<upload-img
								:file-size="10"
								v-model:imageUrl="form[`picUrl${index}`]"
								uploadFileUrl="/admin/ding/feed/upload"
								v-if="index === 1 || form[`picUrl${index - 1}`] !== undefined"
							></upload-img>
						</span>
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item label="内容类型" prop="contentType">
						<el-select v-model="form.contentType" placeholder="请选择内容类型">
							<el-option label="链接" value="link"></el-option>
							<el-option label="文本" value="text"></el-option>
						</el-select>
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item label="描述" prop="description">
						<el-input type="textarea" row="5" v-model="form.description" placeholder="请输入内容" />
					</el-form-item>
				</el-col>

				<el-col v-if="form.contentType == 'text'" :span="24" class="mb20">
					<el-form-item label="内容" prop="content">
						<el-input v-model="form.content" placeholder="请输入内容" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">取 消</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">确 认</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="FdFeedInfoDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from '/@/hooks/message';
import { getObj, addObj, putObj, validateExist } from '/@/api/admin/fdFeedInfo';
import { rule } from '/@/utils/validate';
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);
// const pics = ref(['picUrl1', 'picUrl2', 'picUrl3', 'picUrl4', 'picUrl5', 'picUrl6'])
// 定义字典

// 提交表单数据
const form = reactive({
	id: '',
	feedType: '',
	title: '',
	picUrl: '',
	content: '',
	contentType: '',
	description: '',
	beginTime: '',
	endTime: '',
	authName: '',
	authAvatar: '',
});

// 定义校验规则
const dataRules = ref({});

// 打开弹窗
const openDialog = (id: string) => {
	visible.value = true;
	form.id = '';

	// 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

	// 获取fdFeedInfo信息
	if (id) {
		form.id = id;
		getFdFeedInfoData(id);
	}
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	let picUrl = [];
	for (let i = 0; i < 6; i++) {
		if (!form[`picUrl${i + 1}`] !== true) {
			console.log(`picUrl${i + 1}`, form[`picUrl${i + 1}`]);
			picUrl.push(form[`picUrl${i + 1}`]);
		}
	}

	form.picUrl = picUrl.join(';');

	form.authAvatar = 'https://citycdn.oss-cn-hangzhou.aliyuncs.com/images/logo2.png';
	form.authName = '芜湖本地生活';

	try {
		loading.value = true;
		form.id ? await putObj(form) : await addObj(form);
		useMessage().success(form.id ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

// 初始化表单数据
const getFdFeedInfoData = (id: string) => {
	// 获取数据
	loading.value = true;
	getObj({ id: id })
		.then((res: any) => {
			let result = res.data[0];
			result.picUrl.split(';').forEach((item, index) => {
				result[`picUrl${index + 1}`] = item;
			});
			Object.assign(form, result);
		})
		.finally(() => {
			loading.value = false;
		});
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
